package com.erp.module.yuliaofreight.inYuliaoGiFreight

import androidx.appcompat.app.AlertDialog
import android.view.View
import com.erp.R
import com.erp.activity.BaseActivity
import com.erp.module.yuliaofreight.YuliaoFreightConteract
import com.erp.myAdapter.ArrayAdapter
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_in_yuliao_freight.*
import kotlinx.android.synthetic.main.activity_in_yuliao_freight.AtCangwei
import kotlinx.android.synthetic.main.header_knoya.*

class inYuliaoGiFreightActivity : BaseActivity(), YuliaoFreightConteract.inYuliaoGiFreightView, View.OnClickListener {

    var cangweinames: MutableList<String> = ArrayList<String>()
    var cangweiids: MutableList<String> = ArrayList<String>()
    var bianmaids: MutableList<String> = ArrayList<String>()
    var bianmanames: MutableList<String> = ArrayList<String>()
    lateinit var P: IinYuliaoGiFreightP
    var updialog: AlertDialog?=null
    var rucangcode = ""
    override fun getLayoutId(): Int {
        return R.layout.activity_in_yuliao_freight
    }

    override fun initCreate() {
        rucangcode = intent.getStringExtra("rucangcode")
        tv_header_title.text = "余料入仓"
        iv_header_left.visibility = View.VISIBLE
        iv_header_left.setOnClickListener(this)
        P = IinYuliaoGiFreightP(this,this)
        val listsp = ArrayList<String>()
        listsp.add("板材")
        listsp.add("封边条")
        listsp.add("贴纸")
        listsp.add("吸塑")
        listsp.add("型材")
        listsp.add("包装物")
        val adapter = ArrayAdapter(this, R.layout.lv_sp_tvitem, listsp)
        SpLeibie.adapter = adapter
        BtQueding.setOnClickListener(this)
        BtYuliao.setOnClickListener(this)
        P.getCangweiData()

    }

    override fun inYuliaoChucangOk(data: String) {
        updialog?.dismiss()
        Myutil.MyTost("余料入仓成功")
        AtBianma.setText("")
    }

    override fun inYuliaoChucangNo(error: String) {
        updialog?.dismiss()
        Myutil.MyTost("余料入仓失败")
        AtBianma.setText("")
    }

    override fun onClick(v: View?) {
        when(v){
            iv_header_left -> {
                finish()
            }
            BtQueding -> {
                var yfs_id = ""
                var gi_id = ""
                if (cangweinames.contains(AtCangwei.text.toString())) {
                    cangweinames.indices
                            .filter { cangweinames[it] == AtCangwei.text.toString() }
                            .forEach { yfs_id = cangweiids[it] }
                }
                if(yfs_id == ""){
                    Myutil.MyTost("请先输入仓位")
                }
                if (bianmanames.contains(AtBianma.text.toString())) {
                    bianmanames.indices
                            .filter { bianmanames[it] == AtBianma.text.toString() }
                            .forEach { gi_id = bianmaids[it] }
                }
                if(gi_id == ""){
                    Myutil.MyTost("请先输入余料")
                }
                updialog = Myutil.showupdatedialog(this, "正在获取入仓", false)
                P.inYuliaoRucang(yfs_id,gi_id,EtLenght.text.toString(),EtWidth.text.toString(),EtThickness.text.toString(),rucangcode)
            }
            BtYuliao -> {
                updialog = Myutil.showupdatedialog(this, "正在获取余料数据", false)
                P.getBianmaData(SpLeibie.selectedItem.toString())
            }
        }
    }

    override fun getCangwei(cangweiid: MutableList<String>, cangweiname: MutableList<String>) {
        updialog?.dismiss()
        this.cangweiids = cangweiid
        this.cangweinames = cangweiname
        val av = ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, cangweinames)
        AtCangwei.setAdapter(av)
        AtCangwei.setOnClickListener { AtCangwei.showDropDown() }
    }
    override fun getBianma(bianmaid: MutableList<String>, bianmaname: MutableList<String>) {
        updialog?.dismiss()
        this.bianmaids = bianmaid
        this.bianmanames = bianmaname
        val av = ArrayAdapter(this, R.layout.lv_sp_tvitem, bianmanames)
        AtBianma.setAdapter(av)
        AtBianma.setOnClickListener { AtBianma.showDropDown() }
        AtBianma.setText("")
    }
}